package com.cskaoyan.config;

import com.cskaoyan.interceptor.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author stone
 * @date 2022/12/26 11:14
 */
//@Configuration
@ComponentScan("com.cskaoyan.controller")
@EnableWebMvc // mvc:annotation-driven
// 后面还会做一些和SpringMVC相关的配置 需要实现接口 WebMvcConfigurer，如果实现接口里的方法就是提供一些额外的配置信息
public class ServletConfiguration implements WebMvcConfigurer {
    /*@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 注册ResourceHandler使用ResourceHandlerRegistry
        // addResourceHandler配置ResourceHandler的映射范围
        // addResourceLocations告知当前ResourceHandler你的资源文件处于什么位置
        // ** 代表多级任意url
        // localhost:8080/pic/banner.png
        // localhost:8080/pic/dlrb.jpg
        // location:
        // 1. / web资源路径
        // 2. classpath:/  类加载路径
        // 3. file:文件路径/  文件路径 → 建议（要求）使用的路径
        registry.addResourceHandler("/pic/**").addResourceLocations("/");
        registry.addResourceHandler("/pic2/**").addResourceLocations("classpath:/");
        registry.addResourceHandler("/pic3/**").addResourceLocations("file:D:/tmp/");
    }*/
    @Autowired
    HandlerInterceptor handlerInterceptor1;

    // HandlerInterceptor的实例，可以直接调用构造方法初始化；也可以从容器中取出
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //registry.addInterceptor(new HandlerInterceptor1()).addPathPatterns("/hello");
        registry.addInterceptor(handlerInterceptor1).addPathPatterns("/hello");
        registry.addInterceptor(new HandlerInterceptor2()).addPathPatterns("/hello/**");
        registry.addInterceptor(new HandlerInterceptor3()).addPathPatterns("/goodbye");
        registry.addInterceptor(new HandlerInterceptor4()).addPathPatterns("/goodbye/**");
        registry.addInterceptor(new HandlerInterceptor5()).addPathPatterns("/**");
    }
}
